import request from '@/utils/request';
import { baseurl } from './baseurl';
import { hasIsSw,isLianxiSite} from '../utils/utils';
export interface addSubjectParams {
  title: string;
  level: string;
  num: string;
  pid: string;
  is_sw: string;
}
export async function addSubject(params: addSubjectParams) {
  const data = new FormData();
  data.append('level', params.level);
  data.append('title', params.title);
  data.append('num', params.num);
  data.append('pid', params.pid);
  if (hasIsSw()&&!isLianxiSite()) {
    data.append('is_sw', params.is_sw);
  }
  return request(`${baseurl}index/index/add_subject`, {
    method: 'POST',
    data,
  });
}

export async function getSubject() {
  /* 科目信息获取科目 */
  const data = new FormData();
  if (hasIsSw()&&!isLianxiSite()) {
    if (location.href.includes('xamInfo')||location.href.includes('eoples')) {
      if (location.href.includes('tzExamInfo')||location.href.includes('tzPeoples')) {
        data.append('is_sw', '1');
      } else {
        data.append('is_sw', '2');
      }
    } else {
      const is_sw = JSON.parse(localStorage.getItem('userinfo') || '{}').is_sw;
      data.append('is_sw', is_sw);
    }
  }

  return request(`${baseurl}index/index/subject`, {
    method: 'POST',
    data,
  });
}
export async function getAllSubject(is_sw) {
  /* 科目信息获取科目 */
  const data = new FormData();
  data.append('is_sw', is_sw);
  return request(`${baseurl}index/index/subject`, {
    method: 'POST',
    data,
  });
}

export interface changeSubjectParams {
  id: string;
  title: string;
  num: string;
  level: string;
  pid: string;
  is_sw: string;
}
export async function changeSubject(params: changeSubjectParams) {
  /* 科目信息改变科目 */
  const data = new FormData();
  data.append('id', params.id);
  data.append('type', '1');
  data.append('title', params.title);
  data.append('num', params.num);
  data.append('level', params.level);
  data.append('pid', params.pid);
  if (hasIsSw()&&!isLianxiSite()) {
    data.append('is_sw', params.is_sw);
  }
  return request(`${baseurl}index/index/up_subject`, {
    method: 'POST',
    data,
  });
}

export interface deleteSubjectParams {
  id: string;
}
export async function deleteSubject(params: deleteSubjectParams) {
  /* 科目信息删除科目 */
  const data = new FormData();
  data.append('id', params.id);
  data.append('type', '2');
  return request(`${baseurl}index/index/up_subject`, {
    method: 'POST',
    data,
  });
}

export interface checkSubjectTopicParams {
  id: string;
}
export async function checkSubjectTopic(params: checkSubjectTopicParams) {
  /* 检测分类下是否有试题 */
  const data = new FormData();
  data.append('id', params.id);
  return request(`${baseurl}index/index/subject_topic`, {
    method: 'POST',
    data,
  });
}
